Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make: Use ccache for faster compilation #5318

Merged
merged 3 commits into from
Feb 5, 2024

Conversation

JohannesGaessler
Copy link
Collaborator

On master cmake can make use of ccache to speed up compilation. This PR adds ccache support for make. On my systems compilation time changes like this:

CPU Command Runtime master [s] Runtime PR [s] Speedup
Ryzen 3700X make clean && time make -j 16 40.36 27.31 1.48
Ryzen 3700X make clean && time make -j 16 LLAMA_CUBLAS=1 42.69 28.87 1.48
Xeon E5-2683 v4 make clean && time make -j 32 64.27 46.05 1.40
Xeon E5-2683 v4 make clean && time make -j 32 LLAMA_CUBLAS=1 68.61 46.06 1.49
Xeon E5-2683 v4 make clean && time make -j 32 LLAMA_HIPBLAS=1 83.85 46.04 1.82

The runtime is for a second compilation with 100% cache hit rate; in actual use the speedup will be lower. More generally, while this PR does work to make compilation faster I'm not sure I did everything right. With cmake compilation with 100% hit rate takes less than 5 seconds and I don't understand why make is still so much slower.

@slaren
Copy link
Collaborator

slaren commented Feb 4, 2024

Some results with make clean; time make -j XX:

master ccache
13900k 27.73 secs 18.66 secs
M3 Max 14.72 secs 9.98 secs

Admittedly the M3 Max is not compiling exactly the same code, but it is absurd how much faster it is.

@slaren
Copy link
Collaborator

slaren commented Feb 4, 2024

I suspect that the difference with cmake is that it builds all the source files separately and then links. Changing the main rule in the Makefile to do something similar to that such as this, reduces the build time of make main from 1.4 secs to 150 ms:

main: examples/main/main.cpp                                  ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
	$(CXX) $(CXXFLAGS) -c examples/main/main.cpp -o main.o
	$(CXX) $(CXXFLAGS) ggml.o llama.o console.o $(COMMON_DEPS) $(OBJS) main.o -o $@ $(LDFLAGS)

@slaren
Copy link
Collaborator

slaren commented Feb 4, 2024

One problem with this is that it messes up the compiler identification in build-info:

main: build = 2059 (89cba479)
main: built with ccache version 4.8.2 for
main: seed  = 1707069976

Easy to fix:

@@ -717,7 +720,7 @@ swift: examples/batched.swift
 endif

 common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
-       @sh scripts/build-info.sh $(CC) > [email protected]
+       @sh scripts/build-info.sh "$(CC)" > [email protected]
        @if ! cmp -s [email protected] $@; then \
                mv [email protected] $@; \
        else \

@JohannesGaessler
Copy link
Collaborator Author

Thanks for the help. I rewrote the Makefile to compile each .c/.cpp/.cu to an object file first so it can be cached. The runtime on my systems is now:

CPU Command Runtime master [s] Runtime PR [s] Speedup
Ryzen 3700X make clean && time make -j 16 40.36 0.47 85.87
Ryzen 3700X make clean && time make -j 16 LLAMA_CUBLAS=1 42.69 0.66 64.68
Xeon E5-2683 v4 make clean && time make -j 32 64.27 0.38 169.13
Xeon E5-2683 v4 make clean && time make -j 32 LLAMA_CUBLAS=1 68.61 0.73 93.99
Xeon E5-2683 v4 make clean && time make -j 32 LLAMA_HIPBLAS=1 83.85 0.73 114.86

To avoid spamming the root directory with object files they're put in the same directory as the source files. They're removed in make clean via find (which should be available on pretty much every system).

@JohannesGaessler JohannesGaessler merged commit 098f6d7 into ggerganov:master Feb 5, 2024
53 checks passed
jordankanter pushed a commit to jordankanter/llama.cpp that referenced this pull request Mar 13, 2024
* make: Use ccache for faster compilation
hodlen pushed a commit to hodlen/llama.cpp that referenced this pull request Apr 1, 2024
* make: Use ccache for faster compilation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants